//定义页码
var page1 = 1;
var page2 = 1;
var size = 2;//每页显示2条数据

$(function (){
    $(".btn4").click(function () {
        var cid = $(".cid").val();
        var cname = $(".cname").val();
        var ctel = $(".ctel").val();
        var caddr = $(".caddr").val();
        var ccardid = $(".ccardid").val();
        var centryday = $(".centryday").val();
        var csalary = $(".csalary").val();
        $.post("../gil/updateCanteenMan",{"cid":cid,"cname":cname,"ctel":ctel,"caddr":caddr,"ccardid":ccardid,"centryday":centryday,"csalary":csalary},function (data){
            if (data.code == 200 && data.data > 0){
                alert("添加成功")
            }
        });
        cancelData();
        window.location.reload();
    });
    $(".btn3").click(function () {
        var cid = $(".cid").val();
        var cname = $(".cname").val();
        var ctel = $(".ctel").val();
        var caddr = $(".caddr").val();
        var ccardid = $(".ccardid").val();
        var centryday = $(".centryday").val();
        var csalary = $(".csalary").val();
        $.post("../gil/addCanteenMan",{"cname":cname,"ctel":ctel,"caddr":caddr,"ccardid":ccardid,"centryday":centryday,"csalary":csalary},function (data){
            if (data.code == 200 && data.data > 0){
                alert("添加成功")
            }
        });
        cancelData();
        window.location.reload();
    });
    $(".btn6").click(function () {
        var aid = $(".aid").val();
        var aname = $(".aname").val();
        var atel = $(".atel").val();
        var aaddr = $(".aaddr").val();
        var acardid = $(".acardid").val();
        var ahireday = $(".ahireday").val();
        var asalary = $(".asalary").val();
        $.post("../gil/updateAunt",{"aid":aid,"aname":aname,"atel":atel,"aaddr":aaddr,"acardid":acardid,"ahireday":ahireday,"asalary":asalary},function (data){
            if (data.code == 200 && data.data > 0){
                alert("添加成功")
            }
        });
        cancelData();
        window.location.reload();
    });
    $(".btn5").click(function () {
        var aid = $(".aid").val();
        var aname = $(".aname").val();
        var atel = $(".atel").val();
        var aaddr = $(".aaddr").val();
        var acardid = $(".acardid").val();
        var ahireday = $(".ahireday").val();
        var asalary = $(".asalary").val();
        $.post("../gil/addAunt",{"aname":aname,"atel":atel,"aaddr":aaddr,"acardid":acardid,"ahireday":ahireday,"asalary":asalary},function (data){
            if (data.code == 200 && data.data > 0){
                alert("添加成功")
            }
        });
        cancelData();
        window.location.reload();
    });
});
let second = new Vue({
   el:"#second",
   data:{
       //食堂伙计
       canteenMans:{},
       //阿姨
       aunts:{},
       //伙计数量
       cantennManTotal:0,
       //伙计总页数
       cantennManTotalPage:0,
       //阿姨数量
       auntTotal:0,
       //阿姨页数
       auntTotalPage:0

   },
   mounted:function (){
        this.findCanteenMansByPage();
        this.findAuntByPage();
   },
    methods:{
        findCanteenMansByPage:function(){
            var startNum = (page1 -1) * size;
            axios.post("../gil/findCanteenMansByPage",qs.stringify({"startNum":startNum,"size":size})).then( rs => {
                if (rs.status == 200){
                    if (rs.data.code == 200){
                        this.canteenMans = rs.data.data;
                        this.findCanteenManTotal();
                    }
                }
            });
        },
        findCanteenManTotal:function(){
            axios.get("../gil/findCanteenManTotal").then( rs => {
                if (rs.status == 200){
                    if (rs.data.code == 200){
                        this.cantennManTotal = rs.data.data;
                        this.cantennManTotalPage = Math.ceil(this.cantennManTotal / size || 1);
                        //生成页码
                        $("#pagination1").showPage((Math.ceil(this.cantennManTotal / size) || 1),1);
                    }
                }
            })
        },
        findAuntByPage:function(){
            var startNum = (page2 -1) * size;
            axios.post("../gil/findAuntByPage",qs.stringify({"startNum":startNum,"size":size})).then( rs => {
                if (rs.status == 200){
                    if (rs.data.code == 200){
                        this.aunts = rs.data.data;
                        this.findAuntTotal();
                    }
                }
            });
        },
        findAuntTotal:function(){
            axios.get("../gil/findAuntTotal").then( rs => {
                if (rs.status == 200){
                    if (rs.data.code == 200){
                        this.auntTotal = rs.data.data;
                        this.auntTotalPage = Math.ceil(this.auntTotal / size || 1);
                        //生成页码
                        $("#pagination2").showPage((Math.ceil(this.auntTotal / size) || 1),2);
                    }
                }
            })
        }
    }
});
function cancelData() {
    //更新数据的框关闭
    $(".tts").css({display: 'none'});
    //蒙版消失
    $(".mask").css({display: 'none'});
};
function addCanteenMan(){
    $(".tts1").css({display: 'block', opacity: '1', zIndex: '6'});
    $(".mask").css({display: 'block'});
    $(".btn3").css({display:'block'});
    $(".btn4").css({display:'none'});
}
function addAunt(){
    $(".tts2").css({display: 'block', opacity: '1', zIndex: '6'});
    $(".mask").css({display: 'block'});
    $(".btn5").css({display:'block'});
    $(".btn6").css({display:'none'});
}
//点击食堂伙计那里的修改
function updateCanteenMan(obj){
    $(".tts1").css({display: 'block', opacity: '1', zIndex: '6'});
    $(".mask").css({display: 'block'});
    $(".btn3").css({display:'none'});
    $(".btn4").css({display:'block'});
    var cid = $(obj).parent().siblings().eq(0).text();
    var cname = $(obj).parent().siblings().eq(1).text();
    var ctel = $(obj).parent().siblings().eq(2).text();
    var caddr = $(obj).parent().siblings().eq(3).text();
    var ccardid = $(obj).parent().siblings().eq(4).text();
    var centryday = $(obj).parent().siblings().eq(5).text();
    var csalary = $(obj).parent().siblings().eq(6).text();
    $(".cid").val(cid);
    $(".cname").val(cname);
    $(".ctel").val(ctel);
    $(".caddr").val(caddr);
    $(".ccardid").val(ccardid);
    $(".centryday").val(centryday);
    $(".csalary").val(csalary);
}
//点击食堂伙计那里的修改
function updateAunt(obj){
    $(".tts2").css({display: 'block', opacity: '1', zIndex: '6'});
    $(".mask").css({display: 'block'});
    $(".btn5").css({display:'none'});
    $(".btn6").css({display:'block'});
    var aid = $(obj).parent().siblings().eq(0).text();
    var aname = $(obj).parent().siblings().eq(1).text();
    var atel = $(obj).parent().siblings().eq(2).text();
    var aaddr = $(obj).parent().siblings().eq(3).text();
    var acardid = $(obj).parent().siblings().eq(4).text();
    var ahireday = $(obj).parent().siblings().eq(5).text();
    var asalary = $(obj).parent().siblings().eq(6).text();
    $(".aid").val(aid);
    $(".aname").val(aname);
    $(".atel").val(atel);
    $(".aaddr").val(aaddr);
    $(".acardid").val(acardid);
    $(".ahireday").val(ahireday);
    $(".asalary").val(asalary);
}
function  delCanteenMan(obj){
    var cid = $(obj).parent().siblings().eq(0).text();
    $.post("../gil/delCanteenMan",{"cid":cid},res => {
        window.location.reload();
    });
}
function  delAunt(obj){
    var aid = $(obj).parent().siblings().eq(0).text();
    $.post("../gil/delAunt",{"aid":aid},res => {
        window.location.reload();
    });
}
//点击页码时需要展示的数据
function showToPage(obj,type){
    //获取页码
    var page = $(obj).text();
    var startNum = (page - 1) * size;
    if (type == 1){
        page1 = page;
        //说明是点击的是护理记录那里的页码
        $.post("../gil/findCanteenMansByPage",{"startNum":startNum,"size":size},res => {
            if (res.code == 200){
                second.$data.canteenMans = res.data;
            }
        });
    } else {
        page2 = page;
        $.post("/gil/findAuntByPage",{"startNum":startNum,"size":size},res => {
            if (res.code == 200){
                second.$data.aunts = res.data;
            }
        });
    }
}
//上一页
function previousPage(type){
    if (type ==1){
        if (page1 <= 1){
            return;
        }
        page1 = page1 - 1;
        var startNum = (page1 -1 ) * size;
        $.post("../gil/findCanteenMansByPage",{"startNum":startNum,"size":size},res => {
            if (res.code == 200){
                second.$data.canteenMans = res.data;
            }
        });
    } else {
        if (page2 <= 1){
            return;
        }
        page2 = page2 - 1;
        var startNum = (page2 -1 ) * size;
        $.post("../gil/findAuntByPage",{"startNum":startNum,"size":size},res => {
            if (res.code == 200){
                second.$data.aunts = res.data;
            }
        });
    }
}
//下一页
function nextPage(type){
    if (type == 1){//说明点击的食堂伙计那里的页码
        if (page1 >= second.$data.cantennManTotalPage || page1 <=1){
            return;
        }
        page1 = page1 + 1;
        var startNum = (page1 -1 ) * size;
        $.post("../gil/findCanteenMansByPage",{"startNum":startNum,"size":size},res => {
            if (res.code == 200){
                second.$data.canteenMans = res.data;
            }
        });
    } else {//说明点击的是清洁阿姨那里的页码
        if (page2 >= second.$data.auntTotalPage || page2 <=1){
            return;
        }
        page2 = page2 + 1;
        var startNum = (page2 -1 ) * size;
        $.post("../gil/findAuntByPage",{"startNum":startNum,"size":size},res => {
            if (res.code == 200){
                second.$data.aunts = res.data;
            }
        });
    }
}
//动态生成页码的方法
$.fn.showPage = function(count,type) {

    var str = ' <li><a href="#" aria-label="Previous" onclick="previousPage('+type+')"><span aria-hidden="true">&laquo;</span></a></li>';
    str += '<li><a href="javascript:void(0)" onclick="showToPage(this,'+type+')">1</a></li>';
    for (var i = 2; i <= count; i ++) {
        str += '<li><a href="javascript:void(0)" onclick="showToPage(this,'+type+')">' + i + '</a></li>';
    }
    str += ' <li><a href="#" aria-label="Next" onclick="nextPage('+type+')"><span aria-hidden="true">&raquo;</span></a></li>';
    var currentObj = $(this);
    currentObj.html("").append($(str));
}